package ui;

import log.ILogSteam;
import log.LogItem;
import log.LogItemList;
import sun.swing.SwingUtilities2;

import javax.swing.*;
import javax.swing.SwingUtilities;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.TableModel;
import java.util.ArrayList;
import java.util.List;


public class LogTableModel implements TableModel
{

    @Override
    public int getRowCount() {
        return _logItemList.getLogList().size();
    }

    @Override
    public int getColumnCount() {
        return LogItem.getFieldCount();
    }

    @Override
    public String getColumnName(int columnIndex) {
        return LogItem.getFieldName(columnIndex);
    }

    @Override
    public Class<?> getColumnClass(int columnIndex) {
        return String.class;
    }

    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        return false;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        ArrayList<LogItem> itemList = _logItemList.getLogList();
        LogItem item = itemList.get(rowIndex);
        return item.getFieldByIndex(columnIndex);
    }

    @Override
    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {

    }

    @Override
    public void addTableModelListener(TableModelListener l) {
        _listeners.add(l);
    }

    @Override
    public void removeTableModelListener(TableModelListener l) {
        _listeners.remove(l);

        //TableModelEvent event;

        //l.tableChanged();
    }

    public void addLogStream(ILogSteam stream)
    {
        _logItemList.attachStream(stream);
        _logItemList.SetEventListener(new LogItemList.EventListener() {
            @Override
            public void OnLogChanged(LogItemList host, List<LogItem> newItems) {

                int totalCount = host.getLogList().size();
                int newCount = newItems.size();
                TableModelEvent event = new TableModelEvent(LogTableModel.this, totalCount - newCount - 1, totalCount - 1, 0, TableModelEvent.INSERT);
                for (TableModelListener l : _listeners) {
                    l.tableChanged(event);
                }
            }
        });
    }

    private final LogItemList _logItemList = new LogItemList();
    private final ArrayList<TableModelListener> _listeners = new ArrayList<>();
}

/* SwingWorker
* SwingWorker worker = new SwingWorker<ImageIcon[], Void>() {
    @Override
    public ImageIcon[] doInBackground() {
        final ImageIcon[] innerImgs = new ImageIcon[nimgs];
        for (int i = 0; i < nimgs; i++) {
            innerImgs[i] = loadImage(i+1);
        }
        return innerImgs;
    }

    @Override
    public void done() {
        //Remove the "Loading images" label.
        animator.removeAll();
        loopslot = -1;
        try {
            imgs = get();
        } catch (InterruptedException ignore) {}
        catch (java.util.concurrent.ExecutionException e) {
            String why = null;
            Throwable cause = e.getCause();
            if (cause != null) {
                why = cause.getMessage();
            } else {
                why = e.getMessage();
            }
            System.err.println("Error retrieving file: " + why);
        }
    }
};
*
* */
